import Vue from 'vue'
import Router from 'vue-router'
import homePage from '@/page/homePage'
import titleNav from '@/page/nav/header'
import leftNav from '@/page/nav/leftNav'
import waringInfoList from '@/page/waringInfo/waringInfo'
import mySet from '@/page/mySet'
import photoshow from '@/page/photomanager/photoshow'
import hwstudy from '@/page/photomanager/hwstudy'
import deviceList from '@/page/deviceManage/adminHome'
import data from '@/page/dataHistory/data'
import historyData from '@/page/dataHistory/historyData'
import instructHistory from '@/page/dataHistory/instructControllerHistory.vue'
import selfControlStatus from '@/page/dataHistory/selfControlStatus.vue'
import userHome from '@/page/userHome'
import userDeviceList from '@/page/user/userHome'
import userStrategies from '@/page/setting/userStrategies'
import login from '@/page/login/login'
import timerList from "@/page/dataHistory/timerList"
import deviceStatistic from "@/page/statistics/deviceStatistic";
import byDevice from "@/page/statistics/energyConsumption/byDevice";
import byBlock from "@/page/statistics/energyConsumption/byBlock";
import statistics from "@/page/statistics/statistics";
import monthly from "@/page/statistics/energyConsumption/details/monthly";
import quarterly from "@/page/statistics/energyConsumption/details/quarterly";
import year from "@/page/statistics/energyConsumption/details/year";

Vue.use(Router)


const router = new Router({
   mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/login',
      component: login,
    },
    {
      path: '/admin',
      type: 'admin',
      name: 'admin',
      component: homePage,
      meta: {
        isLogin: true,  // 添加该字段，表示进入这个路由是需要登录的
      },
      redirect: '/admin/adminHome',
      children: [
        {
          path: '/admin/waringInfo',
          name: '报警通知',
          components: {
            default: data,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: false,
          children: [
            {
            path: '/admin/waringInfo',
            name: '通知详情',
            component: waringInfoList,
          },
            {
              path: '/admin/adminHome',
              name: '设备监测',
              component: deviceList,
              childNode: true,
              needData: false
            },

            {
              path: '/admin/photomanager',
              name: '图形分析',
             component:photoshow,
              children: [
                {
                path: '/admin/photomanager/photoshow',
                name: '分析结果预览',
              }],
               },
          {
            path: '/admin/photomanager',
            name: '自学习',
           component:hwstudy,
            children: [
              {
              path: '/admin/photomanager/hwstudy',
              name: '红外自学习',
            }],
             }
          ]
        },
        {
          path: '/admin/data',
          name: '历史数据',
          components: {
            default: data,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: false,
          children: [
            {
            path: '/admin/dataHistory/dataHistory',
            name: '历史状态',
            component: historyData
          },
            {
              path: '/admin/dataHistory/instructHistory',
              name: '历史指令',
              component: instructHistory
            },
            {

              path: '/admin/dataHistory/selfControlStatus',
              name: '自控状态',
              component: selfControlStatus
            },
            {
              path: '/admin/dataHistory/timerList',
              name: '定时信息',
              component: timerList
            }]
        },
        {
          path: '/admin/adminSet',
          name: '系统管理',

          childNode: true,
          needData: false,
          needList: true,
          components: {
            default: mySet,
            top: titleNav,
            aside: leftNav
          }
        },
        {
          path: '/admin/statistics',
          name: '统计报表',
          components: {
            default: statistics,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: false,
          children: [{
            path: '/admin/statistics/energyConsumption',
            name: '能耗统计',

            childNode: true,
            needData: true,
            components: {
              default: statistics,
              top: titleNav,
              aside: leftNav
            },
            children: [{
              path: '/admin/statistics/byDevice',
              name: '按设备统计',
              component: byDevice,
            },
              {
                path: '/admin/statistics/byBlock',
                name: '按区域统计',
                component: byBlock
              },
              {
                path: '/admin/statistics/byDevice/monthly',
                name: '',
                component: monthly
              },
              {
                path: '/admin/statistics/byDevice/quarterly',
                name: '',
                component: quarterly
              },
              {
                path: '/admin/statistics/byDevice/year',
                name: '',
                component: year
              }]
          },
            {
              path: '/admin/statistics/export',
              name: '设备统计',
              components:{
                default: deviceStatistic,
                top: titleNav,
                aside: leftNav
              }
            }]
        }
      ]
    },
    {
      path: '/user',
      type: 'user',
      name: 'user',
      component: homePage,
      meta: {
        isLogin: true,  // 添加该字段，表示进入这个路由是需要登录的
      },
      redirect: '/user/userHome',
      children: [
        {
          path: '/user/userHome',
          name: '实时数据',
          components: {
            default: userDeviceList,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: true
        },
        {
          path: '/user/waringInfo',
          name: '报警通知',
          components: {
            default: waringInfoList,
            top: titleNav,
            aside: leftNav
          },
            childNode: true,
            needData: false,
            children: [{
              path: '/user/waringInfo',
              name: '通知详情',
              component: waringInfoList
            }]
          },
        {
          path: '/user/data',
          name: '历史数据',
          components: {
            default: data,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: false,
          children: [{
            path: '/user/dataHistory/dataHistory',
            name: '历史状态',
            component: historyData
          },
            {
              path: '/user/dataHistory/instructHistory',
              name: '历史指令',
              component: instructHistory
            },
            {
              path: '/user/dataHistory/selfControlStatus',
              name: '自控状态',
              component: selfControlStatus
            }]
        },
        {
          path: '/generalAdmin/statistics',
          name: '统计报表',
          components: {
            default: statistics,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: false,
          children: [{
            path: '/user/statistics/energyConsumption',
            name: '能耗统计',

            childNode: true,
            needData: true,
            components: {
              default: statistics,
              top: titleNav,
              aside: leftNav
            },
            children: [{
              path: '/user/statistics/byDevice',
              name: '按设备统计',
              component: byDevice,
            },
              {
                path: '/user/statistics/byBlock',
                name: '按区域统计',
                component: byBlock
              },
              {
                path: '/user/statistics/byDevice/monthly',
                name: '',
                component: monthly
              },
              {
                path: '/user/statistics/byDevice/quarterly',
                name: '',
                component: quarterly
              },
              {
                path: '/user/statistics/byDevice/year',
                name: '',
                component: year
              }]
          },
            {
              path: '/user/statistics/export',
              name: '设备统计',
              components:{
                default: deviceStatistic,
                top: titleNav,
                aside: leftNav
              }
            }]
        }
      ]
    },
    {
      path: '/generalAdmin',
      type: 'generalAdmin',
      name: 'generalAdmin',
      component: homePage,
      meta: {
        isLogin: true,  // 添加该字段，表示进入这个路由是需要登录的
      },
      redirect: '/generalAdmin/generalAdminHome',
      children: [
        {
          path: '/generalAdmin/waringInfo',
          name: '报警通知',
          components: {
            default: data,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: false,
          children: [{
            path: '/generalAdmin/waringInfo',
            name: '通知详情',
            component: waringInfoList
          },{
            path: '/generalAdmin/generalAdminHome',
            name: '设备监测',
            components: {
              default: deviceList,
              top: titleNav,
              aside: leftNav
            },
            childNode: true,
            needData: false
          },]
        },
        {
          path: '/generalAdmin/data',
          name: '历史数据',
          components: {
            default: data,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: false,
          children: [{
            path: '/generalAdmin/dataHistory/dataHistory',
            name: '历史状态',
            component: historyData
          },
            {
              path: '/generalAdmin/dataHistory/instructHistory',
              name: '历史指令',
              component: instructHistory
            },
            {

              path: '/generalAdmin/dataHistory/selfControlStatus',
              name: '自控状态',
              component: selfControlStatus
            },
            {
              path: '/generalAdmin/dataHistory/timerList',
              name: '定时信息',
              component: timerList
            }]
        },
        {
          path: '/generalAdmin/statistics',
          name: '统计报表',
          components: {
            default: statistics,
            top: titleNav,
            aside: leftNav
          },
          childNode: true,
          needData: false,
          children: [{
            path: '/generalAdmin/statistics/energyConsumption',
            name: '能耗统计',

            childNode: true,
            needData: true,
            components: {
              default: statistics,
              top: titleNav,
              aside: leftNav
            },
            children: [{
              path: '/generalAdmin/statistics/byDevice',
              name: '按设备统计',
              component: byDevice,
            },
              {
                path: '/generalAdmin/statistics/byBlock',
                name: '按区域统计',
                component: byBlock
              },
              {
                path: '/generalAdmin/statistics/byDevice/monthly',
                name: '',
                component: monthly
              },
              {
                path: '/generalAdmin/statistics/byDevice/quarterly',
                name: '',
                component: quarterly
              },
              {
                path: '/generalAdmin/statistics/byDevice/year',
                name: '',
                component: year
              }]
          },
            {
              path: '/generalAdmin/statistics/export',
              name: '设备统计',
              components:{
                default: deviceStatistic,
                top: titleNav,
                aside: leftNav
              }
            }]
        }
      ]
    }
  ]

})

export default router

router.beforeEach((to, from, next) => { //全局钩子函数
  if (to.meta.isLogin) { //通过此操作可以判断哪些页面需要登录
    if (sessionStorage.getItem('userId')!==null) {
      next();
    } else {
      next({
        path: 'http://172.18.8.8:8080/',
        query: {redirect: to.fullPath}  })
    }
  } else {
    next();
  }

});
